﻿using BigMonitoringWarningApi.Model.ApiResult;
using GYCTOAManagementSystemNewApi.Model;
using GYCTOAManagementSystemNewApi.Model.Enum;
using GYCTOAManagementSystemNewApi.Model.Tool;
using GYCTOAManagementSystemNewApi.Model.ViewModel;
using GYCTOAManagementSystemNewApi.Public;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace GYCTOAManagementSystemNewApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DocumentController : Controller
    {
        private readonly ILogger<DocumentController> _logger;
        DBContext _db;
        PublicTool _publictool;
        public DocumentController(ILogger<DocumentController> logger, DBContext db)
        {
            _logger = logger;
            _db = db;
            _publictool = new PublicTool(_db);
        }
        //private Document GetDocument(int Id)
        //{
        //    return _db.Document.Where(l => l.Id == Id).FirstOrDefault();
        //}
        /// <summary>
        /// 数据查询
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="offset"></param>
        /// <param name="document"></param>
        /// <returns></returns>
        //[HttpPost("GetDocumentDetail")]
        //public JsonResult GetDocumentDetail([FromBody] dynamic formData)
        //{
        //    //(int limit, int offset, Document document)
        //    JToken j_token = JsonConvert.DeserializeObject<JToken>(formData.ToString());
        //    int offset = Convert.ToInt32(j_token["currentPage"]);
        //    int limit = Convert.ToInt32(j_token["pagesize"]);
        //    Document document = JsonConvert.DeserializeObject<Document>(j_token["Document"].ToString());
        //    var sql = "select * from document where 1=1 ";
        //    if (document.DocumentNumber != null)
        //    {
        //        sql += "and DocumentNumber like '%" + document.DocumentNumber + "%' ";
        //    }
        //    if (document.Title != null)
        //    {
        //        sql += "and Title like '%" + document.Title + "%' ";
        //    }
        //    var query = _db.Document.FromSqlRaw(sql);
        //    var list = query.Where(x=>!x.Delete).ToList();
        //    var total = list.Count;
        //    var rows = list.Take(limit * offset).Skip(limit * (offset - 1)).ToList();
        //    //var ret = JsonConvert.SerializeObject(rows, setting);
        //    return Json(new { code = 0, msg = "", count = total, data = rows });
        //}
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="arrDel"></param>
        /// <returns></returns>
        //[HttpPost("DeleteDocumentContent")]
        //public JsonResult DeleteDocumentContent(int[] arrDel)
        //{
        //    var author = HttpContext.Request.Headers["Authorization"].ToString().Split(' ')[1];
        //    var flag = false;
        //    foreach (var t in arrDel)
        //    {
        //        var obj = GetDocument(t);
        //        if (obj == null)
        //        {
        //            return Json(false);
        //        }
        //        obj.Delete = true;
        //        PublicTool.GetOpDataOpId(obj, author);
        //    }
        //    flag = _db.SaveChanges() > 0 ? true : false;
        //    return Json(flag);
        //}
        /// <summary>
        /// 添加文档
        /// </summary>
        /// <param name="document"></param>
        /// <returns></returns>
        //[HttpPost("AddDocumentContent")]
        //public JsonResult AddDocumentContent(Document document)
        //{
        //    var author = HttpContext.Request.Headers["Authorization"].ToString().Split(' ')[1];
        //    document.InputDate = DateTime.Now;
        //    PublicTool.GetOpDataOpId(document,author);
        //    _db.Document.Add(document);
        //    var flag = _db.SaveChanges() > 0 ? true : false;
        //    return Json(new { result = flag ,id = document.Id});
        //}
        /// <summary>
        /// 修改文档
        /// </summary>
        /// <param name="document"></param>
        /// <returns></returns>
        //[HttpPost("UpdateDocumentContent")]
        //public JsonResult UpdateDocumentContent(Document document)
        //{
        //    var author = HttpContext.Request.Headers["Authorization"].ToString().Split(' ')[1];
        //    PublicTool.GetOpDataOpId(document, author);
        //    _db.Document.Update(document);
        //    var flag = _db.SaveChanges() > 0 ? true : false;
        //    return Json(flag);
        //}
        /// <summary>
        /// 获取规章制度与模板文件
        /// </summary>
        /// <returns></returns>
        //[HttpPost("GetSystemAndTemplate")]
        //public JsonResult GetSystemAndTemplate()
        //{
        //    var list = _db.Document.Where(x => !x.Delete && (x.FileType == (int)FileTypeEnum.System || x.FileType == (int)FileTypeEnum.Template)).ToList();
        //    return Json(list);
        //} 
    }
}
